
TOOL.Category		= "Wire - Antennas"
TOOL.Name			= "Transmitter Antenna (Dir)"
TOOL.Command		= nil
TOOL.ConfigName		= ""

if ( CLIENT ) then
    language.Add( "Tool_wire_antenna_txd_name", "Transmitter Antenna Tool (Wire)" )
    language.Add( "Tool_wire_antenna_txd_desc", "Spawns a transmitter antenna for use with the wire system." )
    language.Add( "Tool_wire_antenna_txd_0", "Primary: Create/Update Transmitter Antenna" )
	language.Add( "sboxlimit_wire_tx_antennas", "You've hit transmitter antennas limit!" )
	language.Add( "undone_wireantennatxd", "Undone Wire Transmitter Antenna" )
else
	CreateConVar('sbox_maxwire_tx_antennas', 9000)
end

cleanup.Register( "wire_antenna_txds" )
TOOL.Model = "models/radio/ra_uplink_dish.mdl"

function TOOL:LeftClick( trace )
	if trace.Entity && trace.Entity:IsPlayer() then return false end
	if ( SERVER && !util.IsValidPhysicsObject( trace.Entity, trace.PhysicsBone ) ) then return false end
	if (CLIENT) then return true end
	local ply = self:GetOwner()
	if ( !self:GetSWEP():CheckLimit( "wire_antenna_txds" ) ) then return false end
	if (not util.IsValidModel(self.Model)) then return false end
	if (not util.IsValidProp(self.Model)) then return false end	
	local Ang = trace.HitNormal:Angle()
	wire_antenna_txd = MakeAntennaTXD( ply, Ang, trace.HitPos )
	local min = wire_antenna_txd:OBBMins()
	wire_antenna_txd:SetPos( trace.HitPos - trace.HitNormal * min.x )
	local const = WireLib.Weld(wire_antenna_txd, trace.Entity, trace.PhysicsBone, false)
	wire_antenna_txd:GetPhysicsObject():EnableCollisions(true)
	undo.Create("WireAntenna")
		undo.AddEntity( wire_antenna_txd )
		undo.AddEntity( const )
		undo.SetPlayer( ply )
	undo.Finish()
	ply:AddCleanup( "wire_antenna_txds", wire_antenna_txd )
	ply:AddCleanup( "wire_antenna_txds", const )
	return true
end

if (SERVER) then
	function MakeAntennaTXD( pl, Ang, Pos)
		if ( !pl:CheckLimit( "wire_antenna_txds" ) ) then return false end
		local wire_antenna_txd = ents.Create( "gmod_wire_radio_transmitter" )
		if (!wire_antenna_txd:IsValid()) then return false end
		wire_antenna_txd:SetAngles( Ang )
		wire_antenna_txd:SetPos( Pos )
		wire_antenna_txd:Spawn()
		wire_antenna_txd:SetPlayer( pl )
		pl:AddCount( "wire_antenna_txds", wire_antenna_txd )
		return wire_antenna_txd
	end
	duplicator.RegisterEntityClass("gmod_wire_antenna_txd", MakeAntennaTXD, "Ang", "Pos")
end

function TOOL:UpdateGhostAntenna( ent, player )
	if ( !ent ) then return end
	if ( !ent:IsValid() ) then return end
	local tr = utilx.GetPlayerTrace( player, player:GetCursorAimVector() )
	local trace = util.TraceLine( tr )
	if (!trace.Hit) then return end
	local Ang = trace.HitNormal:Angle()
	local min = ent:OBBMins()
	ent:SetPos( trace.HitPos - trace.HitNormal * min.x )
	ent:SetAngles( Ang )
	ent:SetNoDraw( false )
end


function TOOL:Think()
	if (!self.GhostEntity || !self.GhostEntity:IsValid() || self.GhostEntity:GetModel() != self.Model ) then
		self:MakeGhostEntity( self.Model, Vector(0,0,0), Angle(0,0,0) )
	end
	self:UpdateGhostAntenna( self.GhostEntity, self:GetOwner() )
end

function TOOL.BuildCPanel(panel)
	panel:AddControl("Header", { Text = "#Tool_wire_antenna_txd_name", Description = "#Tool_wire_antenna_txd_desc" })
end
